using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using RadinfMagazynier3.Forms;

namespace RadinfMagazynier3.Tools
{
    class ItemTools
    {
        public static string GenerateCode(string code)
        {
            string generatedCode = string.Empty;
            try
            {
                generatedCode = code.Trim();
                long num = 0;
                DataRow[] rows = MainForm.DataSource.CODE.Select("CODE = " + RadinfTools.EscapeSQL(generatedCode), "NUM DESC");
                if (rows.Length > 0)
                {
                    num = RadinfTools.ConvertToLong(rows[0]["NUM"]);
                    if (num < 1)
                    {
                        RadinfTools.ShowError("ItemTools.GenerateCode(" + code + ")", "Nie wygenerowano unikalnego kodu");
                        return string.Empty;
                    }
                }

                DataRow newRow = MainForm.DataSource.CODE.NewCODERow();
                newRow["CODE"] = generatedCode;
                newRow["NUM"] = ++num;
                MainForm.DataSource.CODE.Rows.Add(newRow);

                generatedCode += "-" + num.ToString();
            }
            catch (Exception exc)
            {
                RadinfTools.ShowError("ItemTools.GenerateCode(" + code + ")", exc.Message);
                RadinfTools.ShowError("ItemTools.GenerateCode(" + code + ")", "Nie wygenerowano unikalnego kodu");
                generatedCode = code;
            }
            return generatedCode;
        }

        public static double GetValueBrutto(double netto, double vat)
        {            
            double brutto;
            
            brutto = netto + Math.Round((netto * vat), 2);
            return brutto;
        }

        public static double GetValueNetto(double brutto, double vat)
        {
            double netto;
            
            netto = Math.Round((brutto / (vat + 1)), 2);
            return netto;
        }

        public static double GetMargin(double buyPrice, double sellPrice)
        {
            double margin;
            
            margin = Math.Round((((100 * sellPrice) / buyPrice) - 100), 2);
            return margin;
        }

        public static double GetVatValue(long vatID)
        {
            double value = -1;

            DataRow[] dr = MainForm.DataSource.VAT.Select("ID = " + vatID.ToString());
            if (dr.Length == 1)
            {
                value = RadinfTools.ConvertToLong(dr[0]["VAL"]);
                value /= 100;
            }
            else
            {
                RadinfTools.ShowError("ItemTools.GetVatValue(" + vatID.ToString() + ")", "Nie odnaleziono stawki VAT");
            }
            return value;
        }

        public static long GetVatID(long vatValue)
        {
            long vatID = -1;

            DataRow[] dr = MainForm.DataSource.VAT.Select("VAL = " + vatValue.ToString());
            if (dr.Length == 1)
            {
                vatID = RadinfTools.ConvertToLong(dr[0]["ID"]);
            }
            else
            {
                RadinfTools.ShowError("ItemTools.GetVatID(" + vatValue.ToString() + ")", "Nie odnaleziono stawki VAT");
            }
            return vatID;
        }

        public static string GetMeasureDescription(long measureID)
        {
            string result = string.Empty; 
            DataRow[] measureDR = MainForm.DataSource.MEASURE.Select("ID = " + measureID.ToString());
            if (measureDR.Length == 1)
            {
                result = RadinfTools.ConvertToString(measureDR[0]["DESC"]);
            }
            else
            {
                RadinfTools.ShowError("ItemTools.GetMeasureDescription(" + measureID.ToString() + ")", "Nie odnaleziono jednostki miary");
            }
            return result;
        }

        public static long GetMeasureID(string measureDescription)
        {
            long result = -1;
            DataRow[] measureDR = MainForm.DataSource.MEASURE.Select("DESC = " + RadinfTools.EscapeSQL(measureDescription));
            if (measureDR.Length == 1)
            {
                result = RadinfTools.ConvertToLong(measureDR[0]["ID"]);
            }
            return result;
        }
    }
}
